package com.hzlx.finishedGoods.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.hzlx.*;
import com.hzlx.ResultDto;
import com.hzlx.finishedGoods.feign.WorkOrderService;
import jakarta.annotation.Resource;

import com.hzlx.ProductionProductsDto;
import com.hzlx.finishedGoods.feign.ProductionService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.hzlx.finishedGoods.mapper.MesFinishedGoodsInventoryMapper;
import com.hzlx.finishedGoods.domain.MesFinishedGoodsInventory;
import com.hzlx.finishedGoods.service.IMesFinishedGoodsInventoryService;

/**
 * 入库信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@Service
public class MesFinishedGoodsInventoryServiceImpl implements IMesFinishedGoodsInventoryService 
{
    @Resource
    private ProductionService productionService;
    @Resource
    WorkOrderService workOrderService;
    @Autowired
    private MesFinishedGoodsInventoryMapper mesFinishedGoodsInventoryMapper;

    /**
     * 查询入库信息管理
     * 
     * @param id 入库信息管理主键
     * @return 入库信息管理
     */
    @Override
    public MesFinishedGoodsInventory selectMesFinishedGoodsInventoryById(Long id)
    {
        return mesFinishedGoodsInventoryMapper.selectMesFinishedGoodsInventoryById(id);
    }

    /**
     * 查询入库信息管理列表
     * 
     * @param mesFinishedGoodsInventory 入库信息管理
     * @return 入库信息管理
     */
    @Override
    public List<MesFinishedGoodsInventory> selectMesFinishedGoodsInventoryList(MesFinishedGoodsInventory mesFinishedGoodsInventory)
    {
        return mesFinishedGoodsInventoryMapper.selectMesFinishedGoodsInventoryList(mesFinishedGoodsInventory);
    }

    /**
     * 新增入库信息管理
     * 
     * @param mesFinishedGoodsInventory 入库信息管理
     * @return 结果
     */
    @Override
    public int insertMesFinishedGoodsInventory(MesFinishedGoodsInventory mesFinishedGoodsInventory)
    {
        ProductionProductsDto productionProductsDto=new ProductionProductsDto();
        productionProductsDto.setProductId(Math.toIntExact(mesFinishedGoodsInventory.getProductFinishedId()));
        productionService.updateByProductIdByFinished(productionProductsDto);
        return mesFinishedGoodsInventoryMapper.insertMesFinishedGoodsInventory(mesFinishedGoodsInventory);
    }

    /**
     * 修改入库信息管理
     * 
     * @param mesFinishedGoodsInventory 入库信息管理
     * @return 结果
     */
    @Override
    public int updateMesFinishedGoodsInventory(MesFinishedGoodsInventory mesFinishedGoodsInventory)
    {
        return mesFinishedGoodsInventoryMapper.updateMesFinishedGoodsInventory(mesFinishedGoodsInventory);
    }

    /**
     * 批量删除入库信息管理
     * 
     * @param ids 需要删除的入库信息管理主键
     * @return 结果
     */
    @Override
    public int deleteMesFinishedGoodsInventoryByIds(Long[] ids)
    {
        return mesFinishedGoodsInventoryMapper.deleteMesFinishedGoodsInventoryByIds(ids);
    }

    /**
     * 删除入库信息管理信息
     * 
     * @param id 入库信息管理主键
     * @return 结果
     */
    @Override
    public int deleteMesFinishedGoodsInventoryById(Long id)
    {
        return mesFinishedGoodsInventoryMapper.deleteMesFinishedGoodsInventoryById(id);
    }

    @Override
    public ResultDto<String> pack(Long id) {
        //根据id查出入库信息
        MesFinishedGoodsInventory product = mesFinishedGoodsInventoryMapper.selectMesFinishedGoodsInventoryById(id);
        System.out.println("当前入库产品信息："+product);
        //1.根据产品id查询对应的工单信息
        List<WorkOrderDto> workList = workOrderService.getInfoDto(product.getProductId())
                .stream()
                .filter(work->work.getStatus() == 2)
                .collect(Collectors.toList());
        System.out.println("正在生产中的工单:"+workList);
        //2.根据工单信息中的优先级，并且确保工单状态为生产中时，筛选出具体工单
        WorkOrderDto workOrder = workList.stream().min(Comparator.comparing(WorkOrderDto::getPriority)).orElse(null);
        System.out.println("优先级最高的工单:"+workOrder);

        //3.将入库的产品归为对应的工单中
        product.setOrderId(workOrder.getOrderNumber());
        product.setIsPackaged(Long.valueOf(1));
        System.out.println(product.getOrderId());
        System.out.println("当前产品"+product);
        mesFinishedGoodsInventoryMapper.updateMesFinishedGoodsInventory(product);
                //取得所有工单为对应id的入库信息+取出工单对应的所有入库产品的已完成的最大值
                List<MesFinishedGoodsInventory> mesFinishedGoodsInventories = mesFinishedGoodsInventoryMapper.selectListByOrderId(product.getOrderId());
                System.out.println("当前工单下的所有产品"+mesFinishedGoodsInventories);
                //下面这段好像没用...
                Long productOrderNum = mesFinishedGoodsInventories
                        .stream()
                        .map(MesFinishedGoodsInventory::getOrderNum)
                        .filter(Objects::nonNull)
                        .max(Comparator.naturalOrder())
                        .orElse(0L);

                System.out.println("当前已经做好了"+workOrder.getActualQuantity()+"个");
                //设置当前product的已完成产品+1
                product.setOrderNum(workOrder.getActualQuantity()+1);
                System.out.println("该产品是该工单的第"+workOrder.getActualQuantity()+"个");
                mesFinishedGoodsInventoryMapper.updateMesFinishedGoodsInventory(product);
        //4.增加工单产品数量
        workOrder.setActualQuantity(workOrder.getActualQuantity()+1);
        //5.根据工单信息中的计划数量和实际数量作比较，若数量足够将工单状态改为待质检
        System.out.println("实际数量: " + workOrder.getActualQuantity() + ", 计划数量: " + workOrder.getPlannedQuantity());
        if (workOrder.getActualQuantity().equals(workOrder.getPlannedQuantity()) ){
            System.out.println("生产数量足够");
            workOrder.setStatus(4);
        }
        System.out.println("工单信息状态"+workOrder.getStatus());
        //6.更改工单信息
        workOrderService.editByMaterial(workOrder);
        return ResultDto.<String>builder().code(200).msg("产品"+product.getProductId()+"包装成功").build();
    }

    @Override
    public List<MesFinishedGoodsInventory> selectListByOrderId(String orderId) {
        return mesFinishedGoodsInventoryMapper.selectListByOrderId(orderId);
    }

    @Override
    public List<MesFinishedGoodsInventory> selectByOrderIdAndOrderNums(String orderId, List<Integer> orderNums) {
        return mesFinishedGoodsInventoryMapper.selectByOrderIdAndOrderNums(orderId,orderNums);
    }

    @Override
    public void insertMesFinishedGoodsInventoryByBadGoods(MesFinishedGoodsInventoryDto mesFinishedGoodsInventoryDto) {
        mesFinishedGoodsInventoryMapper.insertMesFinishedGoodsInventoryByBadGoods(mesFinishedGoodsInventoryDto);
    }
}
